package com.guidance.guidance.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.guidance.guidance.domain.ActivityInformation;
import com.guidance.guidance.domain.FristMessage;
import com.guidance.guidance.domain.Message;
import com.guidance.guidance.repository.ActivityInformationRepository;
import com.guidance.guidance.repository.FristMessageRepository;

@RestController
@RequestMapping("/leaveMessage")
public class LeavingMessageController {
	
	@Autowired
	private FristMessageRepository fristMessageRepository;
	@Autowired
	private ActivityInformationRepository aIR;
	
	/**
	 * 向留言表中插入留言信息
	 * @param msg
	 * @return
	 */
	@RequestMapping("/insertMessage")
	public Message<String> leaveMessage(Message<String> msg,HttpServletRequest request){
		String name = request.getParameter("name");
		String content = request.getParameter("content");
		String act_id = request.getParameter("act_id");
		System.out.println(name + "  "+ content + "  "+ act_id);
		FristMessage fm = new FristMessage();
		List<ActivityInformation> aIF = aIR.findAll();
		for(ActivityInformation a : aIF) {
			if(a.getId()== Integer.valueOf(act_id)) {
				fm.setaI(a);
			}
		}
		fm.setContent(content);
		fm.setName(name);
		fristMessageRepository.save(fm);
		if(fristMessageRepository.save(fm)!= null) {
			msg.setCode("200");
			msg.setMsg("留言成功");
			msg.setSuccess("true");
		}else {
			msg.setCode("404");
			msg.setMsg("留言失败");
			msg.setSuccess("false");
		}
		return msg;
	}
	
	
	/**
	 * 返回该信息对应的所有留言信息
	 * @param msg
	 * @return
	 */
	@RequestMapping("/getMessageList")
	public Message<List> getMessageList(Message<List> msg,HttpServletRequest request){
		String act_id = request.getParameter("act_id");
		aIR.findById(Integer.valueOf(act_id));
		List<FristMessage> fm = fristMessageRepository.findByAI(aIR.findById(Integer.valueOf(act_id)));
		if(fm.size()!=0) {
			msg.setCode("200");
			msg.setSuccess("true");
			msg.setMsg("获取成功");
			msg.setData(fm);
		}else {
			msg.setCode("404");
			msg.setSuccess("false");
			msg.setMsg("获取失败");
		}
		System.out.println(msg);
		return msg;
	}
}
